<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\User;
use App\Models\Luo;

class LuoUser extends Model {
    use SoftDeletes;

    protected $fillable = ['luo_id', 'uid'];
    protected $dates = ['deleted_at'];

    public function user() {
        return $this->hasOne('App\Models\User', 'id', 'uid');
    }

    public function luo() {
        #FIXME: if many to one exist, may cause problem
        return $this->hasOne('App\Models\Luo', 'id', 'luo_id');
    }

    public static function add(User $u, Luo $l) {
        if($u->get_luo() && $u->get_luo() == $l) return;
        LuoUser::where('uid', $u->id)->delete();
        return LuoUser::create(['luo_id' => $l->id, 'uid' => $u->id]);
    }
}
